import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import iview from 'iview'
import 'iview/dist/styles/iview.css'
import qs from 'qs'
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'


Vue.use(iview);
Vue.use(preview)
Vue.config.productionTip = false;
Vue.prototype.$http = axios;
Vue.prototype.$uploadUrl = 'http://47.105.45.14:8080/mechanic/service/fileUpload/uploadImage';
//Vue.prototype.$uploadUrl = 'http://192.168.2.222:8081/mechanic/service/fileUpload/uploadImage';
/**
 * axios全局配置
 */
axios.defaults.timeout = 100000;
axios.defaults.baseURL = 'http://47.105.45.14:8080/mechanic/service'
//axios.defaults.baseURL = 'http://192.168.2.222:8081/mechanic/service'
// axios.defaults.baseURL = 'http://192.168.1.106:8080/service';
// axios.defaults.baseURL = 'http://192.168.1.105:8081/service';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
/**
 * axios全局配置请求拦截
 */
axios.interceptors.request.use((res) => {
	let authority = JSON.parse(sessionStorage.getItem('Authorization'));
	if (authority) {
		res.headers['Authorization'] = authority;
	}
	res.data = qs.stringify(res.data)
	return res
});

/**
 * axios全局配置响应拦截
 */
axios.interceptors.response.use((req) => {
	if (req.status == 200) {
		switch (req.data.code) {
			case '401':
				vm.$router.push('/');
				break;
			default:
				return req.data
		}
	}
	return req.data
}, err => {
	console.dir(err)
	vm.$router.push('/');
});

router.beforeEach((to, from, next) => {
	iview.LoadingBar.start();
	next();
});

router.afterEach(route => {
	iview.LoadingBar.finish();
});

Vue.filter('timeForm', function(value) {
	if (typeof(value) == "undefined" || value === null) return "";

	let date = new Date(value);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	var d = date.getDate();
	if (isNaN(y) && y != 0) {
		y = " ";
	}
	if (isNaN(m) && m != 0) {
		m = " ";
	} else {
		if (m < 10) {
			m = "0" + m
		}
	}
	if (isNaN(d) && d != 0) {
		d = " ";
	} else {
		if (d < 10) {
			d = "0" + d
		}
	}
	return y + "-" + m + "-" + d + " " + date.toTimeString().substr(0, 5);
});

var timeForm = Vue.filter('timeForm');
const vm = new Vue({
	router,
	render: h => h(App)
}).$mount('#app');
